<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .parent {
      position: relative;
      top: 100px;
      left: 100px;
      padding: 100px;
      background-color: rgba(17, 119, 255, 0.5);
    }

    .child {
      width: 100px;
      height: 100px;
      padding: 10px;
      border: 1px solid black;
      margin: 100px;
    }
  </style>
</head>

<body>
  <div class="parent">
    <div class="child"></div>
  </div>

  <script>
    const elem = document.querySelector('.child')

    // offsetWidth/offsetHeight = width/height + padding + border
    console.log('elem.offsetWidth', elem.offsetWidth)
    console.log('elem.offsetHeight', elem.offsetHeight)

    // offsetParent 获取第一个非 static 定位的父元素，默认返回 body 元素
    console.log('elem.offsetParent', elem.offsetParent)

    // offsetTop/offsetLeft 相对于 offsetParent 的位置
    console.log('elem.offsetTop', elem.offsetTop)
    console.log('elem.offsetLeft', elem.offsetLeft)
  </script>
</body>

</html>